﻿/*----------------------------------------------------------*
 *                LaunchBallDirection Class                 *
 *                                                          *
 *          Created By: Matthew Hoyes & Ronald Ram          *
 *                                                          *
 *      This class will be used inside Ball class to        *
 *  calculate the launching ball direction on game Start    *
 *----------------------------------------------------------*/

#region USING STATEMENTS

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics; // For Texture2D
using Microsoft.Xna.Framework;          // For Vector2

#endregion USING STATEMENTS;

namespace BrickBreaker.Core
{   
    public class BBBallDirection : BBSingleSprite
    {
        #region DATA_MEMBERS
        //
        private float currAngle;
        private float angleInRad;
        private Vector2 origin;

        #region CONSTANTS
        private const float ZERO = 0.0f;
        private const float MIN_ANGLE_DEGREES = -60;
        private const float MAX_ANGLE_DEGREES = 60;
        #endregion CONSTANTS
        //
        #endregion DATA_MEMBERS


        #region OBJECT_CONSTRUCTION
        //
        //DEFAULT Constructor
        public BBBallDirection(Texture2D texture) : base(texture, Vector2.Zero)
        {
            this.currAngle = ZERO; //Face Up or ZERO degress
            this.angleInRad = MathHelper.ToRadians(currAngle);

            Init();
        }

        /// <summary> Initializes the ball's properties. </summary>
        private void Init()
        {
            this.currAngle = ZERO;
            base.size = new Vector2(texture.Width, texture.Height);
            base.speed = new Vector2(1.0f);
            base.direction = new Vector2(ZERO, -1); //Face Up or ZERO degress
            this.origin = new Vector2(texture.Width / 2, texture.Height - (texture.Height / 6)); 
        }
        //
        #endregion OBJECT_CONSTRUCTION


        #region PROPERTIES
        public override Vector2 Direction
        {
            get
            {
                float radians = angleInRad - MathHelper.ToRadians(90.0f); 
                base.direction = new Vector2((float)Math.Round(Math.Cos(radians), 2), (float)Math.Round(Math.Sin(radians), 2));
                return base.Direction;
            }
            //set { base.Direction = value; }
        }
        #endregion PROPERTIES



        #region PUBLIC_METHODS
        //
        public override void Reset() { Init(); }

        //Update the Rotation Angle
        public override void Move(GameTime gameTime)
        {
            if ( currAngle <= MIN_ANGLE_DEGREES )
            {
                currAngle = MIN_ANGLE_DEGREES;
                base.speed *= -1;//Vector2.One; //speed becomes +VE
            }
            else if (currAngle >= MAX_ANGLE_DEGREES)
            {
                currAngle = MAX_ANGLE_DEGREES;
                base.speed *= -1.0f; //speed becomes -VE
            }
            //else if (currAngle == ZERO)
            //{
            //    base.speed = Vector2.One; //speed becomes +VE
            //}

            this.currAngle += base.speed.X;
            this.angleInRad = MathHelper.ToRadians(currAngle);
            //do nothing for now...
        }

        public void Update(GameTime gameTime, Vector2 position)
        {
            base.position = position;
            Move(gameTime);
        }

        //
        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(base.texture, base.position, null, Color.White, this.angleInRad, this.origin, 1.0f, SpriteEffects.None, 0.0f);
            //do something
        }
        //
        #endregion PUBLIC_METHODS

    }//end class
}//end namespace
